package com.bigknow.appstore.proxy.handler.dispatcher;

import com.bigknow.appstore.proxy.cache.ICacheManager;
import com.bigknow.appstore.proxy.handler.base.BaseInboundHandler;
import com.bigknow.appstore.proxy.log.ILogWriter;
import com.bigknow.appstore.proxy.server.IProxyServer;
import com.bigknow.appstore.proxy.util.HandlerVariables;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.FullHttpRequest;

/**
 * 将request的uri服务信息等进行重定位
 * Created by chaos on 2016/9/19.
 */
@ChannelHandler.Sharable
public class RequestDispatcherHandler extends BaseInboundHandler{

    public RequestDispatcherHandler(IProxyServer proxyServer, ICacheManager cacheManager, ILogWriter writer) {
        super(proxyServer, cacheManager, writer);
    }

    @Override
    public void read(ChannelHandlerContext ctx, FullHttpMessage msg) throws Exception {
        FullHttpRequest request = (FullHttpRequest) msg;
        request.setUri(HandlerVariables.currentRequestVoLocal.get().getSuffixUri());
        logger.info("real uri is : "+request.uri());
        ctx.fireChannelRead(msg);
    }
}
